// 主域名
var domain = "http://192.168.1.250/";
// 新增收货地址-接口
var add_address_url = domain+"mb/toShowConsigneeAddress.htmls";

$(function(){
    // 页面加载完毕调用接口查询用户的收货地址列表
	var user_id=$.cookie("user_id"),
	if(user_id!=undefined && user_id!=""){
		getUserAddressList();
	}else{
		alert("亲，您还没有登录呦");
		location.href="http://192.168.1.106：8081/yuyan2/login.html";
	}
	
})

function getUserAddressList(){
	showLoading(true,"拉取数据中...");
	
	$.ajax({
		type: "GET",
		url: add_address_url,
		data: {
			user_id:$.cookie("user_id"),
		},
		dataType: "jsonp",
		jsonp: "callbackparam", //服务端用于接收callback调用的function名的参数  
        jsonpCallback: "success_jsonpCallback", //callback的function名称,服务端会把名称和data一起传递回来  
		success: function(json){
			showLoading(false,null);
			//console.debug(json);
			var data = json[0].data;
			var status = data['status'];
			if(1==status){
				showLoading(false, null);
				var map = data['data'];
				var inner_html = "";
				console.debug(map);
				for(var key in map){ 
				    var address_obj = map[key];  
				    // 收货人
				    var id = address_obj['id'];
				    // 收货人
				    var consignee_name = address_obj['consignee_name'];
				    // 收货人电话
				    var mobile = address_obj['mobile'];
				    // 省
				    var province = address_obj['province'];
				    // 市区
				    var city = address_obj['city'];
				    // 县城
				    var town = address_obj['town'];
				    // 详细街道
				    var address_info = address_obj['address_info'];
				    // 地址
					var default_consignee = address_obj['default_consignee'];
					//是否默认
				    var address = province + "-" + city + "-" + town + "-" + address_info;
					//编辑传值
					var getUrl = "?id="+id+"&user_name="+consignee_name+"&mobile="+mobile+"&province="+province+"&city="+city+"&town="+town+"&address_info="+address_info;
					inner_html += "<div id=\"item_"+id+"\" style=\"width: 96%;height: auto;margin-left: 2%;margin-right: 2%;background: white;margin-top: 20px;\">";
					inner_html += "<div style=\"width: 90%;height: 60px;margin-left: 5%;margin-right: 5%;\">";
					inner_html += "<span style=\"font-size: 1.8em;width: 50%;float: left;line-height: 40px;margin-top: 20px;\">"+consignee_name+"</span>";
					inner_html += "<span style=\"font-size: 1.9em;width: 50%;float: right;line-height: 40px;margin-top: 20px;\">"+mobile+"</span>";
					inner_html += "</div>";
					inner_html += "<div style=\"width: 90%;height: 40px;margin-top: 20px;margin-left: 5%;margin-right: 5%;\">";
					inner_html += "<span style=\"font-size: 1.5em;width: 100%;float: left;color: #9A9CA0;line-height: 40px;\">"+address+"</span>";
					inner_html += "</div>";
					inner_html += "<div style=\"width: 90%;height: 1px;margin-top: 60px;border-bottom: 1px dashed #9A9CA0;margin-left: 5%;margin-right: 5%;\"></div>";
					inner_html += "<div style=\"width: 90%;height: 80px;margin-top: 40px;margin-left: 5%;margin-right: 5%;\">";
					inner_html += "<div style=\"width: 50%;height: 60px;float: left;text-align: left;\">";
					if(default_consignee == -1){
						inner_html += "<input class=\"ck\" value=\""+id+"\" type=\"checkbox\" style=\"width: 50px;margin-right: 20px;margin-bottom: 10px;\"><span style=\"font-size: 1.9em;line-height: 60px;\">设为默认</span>";
					}else{
						inner_html += "<input class=\"ck\" value=\""+id+"\" type=\"checkbox\" checked='checked' disabled='disabled' style=\"width: 50px;margin-right: 20px;margin-bottom: 10px;\"><span style=\"font-size: 1.9em;line-height: 60px;\">设为默认</span>";
					}
					inner_html += "</div>";
					inner_html += "<div class=\"sc\" id=\""+id+"\" style=\"width: 25%;height: 60px;float: right;text-align: center;\">";
					inner_html += "<img src=\"./address_delete.png\" style=\"width: 40px;height: 40px;margin-bottom: -5px;margin-right: 10px;\"><span   style=\"font-size: 1.9em;line-height: 60px;\">删除</span>";
					inner_html += "</div>";
					inner_html += "<div class=\"bj\" bj_id=\""+id+"\" getUrl=\""+getUrl+"\" style=\"width: 25%;height: 60px;float: right;text-align: center;\">";
					inner_html += "<img src=\"./address_edit.png\" style=\"width: 40px;height: 40px;margin-bottom: -5px;margin-right: 10px;\"><span style=\"font-size: 1.9em;line-height: 60px;\">编辑</span>";
					inner_html += "</div>";
					inner_html += "</div>";
					inner_html += "</div>";
				}  
				$("#data_area").html(inner_html);
				// 绑定checkbox事件
				checkBoxBind();
				//绑定删除事件
				delBind();
				//绑定编辑事件
				updateBind();
				showLoading(false, null);
			}else{
				showLoading(false, null);
			}
		}
	});
}

function showLoading(flag,msg){
	if(flag){
		$("#loading").show();
		$("#loading_bg").show();
		$("#loading_msg").html(msg);
		window.ontouchmove=function(e){
            e.preventDefault && e.preventDefault();
            e.returnValue=false;
            e.stopPropagation && e.stopPropagation();
            return false;
		}        
	}else{
		window.ontouchmove=function(e){
            e.preventDefault && e.preventDefault();
            e.returnValue=true;
            e.stopPropagation && e.stopPropagation();
            return true;
        }
		$("#loading_bg").hide();
		$("#loading_msg").html("");
		$("#loading").hide();
	}
}

function checkBoxBind(){
	$(".ck").each(function(){
		var checkbox_obj = $(this);
		$(this).click(function(){
			$(".ck").attr('checked',false);
			$(".ck").attr('disabled',false);
			$(this).attr('checked','checked');
			$(this).attr('disabled','disabled');
			var id = checkbox_obj.val();
			// 主域名
			var domain = "http://192.168.1.250/";
			// 设为默认-接口
			var add_address_url = domain+"mb/toSetDefaultConsignee.htmls";
			$.ajax({
			type: "GET",
			url: add_address_url,
				data: {
					user_id:$.cookie("user_id"),
					id:id,
					//user_id:$.cookie("user_id"),
				},
				dataType: "jsonp",
				jsonp: "callbackparam", //服务端用于接收callback调用的function名的参数  
				jsonpCallback: "success_jsonpCallback", //callback的function名称,服务端会把名称和data一起传递回来  
				success: function(json){

				}
			});
			shake($("#item_"+id),"red",3);
		});
	});
	
}
function delBind(){
	$(".sc").each(function(){
		var del_obj = $(this);
		$(this).click(function(){
			var id=del_obj.attr('id');
			del_address(id)
			});
		
	})
}
function updateBind(){
		$(".bj").each(function(){
		var bj_obj = $(this);
		$(this).click(function(){
			var id = bj_obj.attr('bj_id');
			var getUrl=bj_obj.attr("getUrl");
			url="update_address.html"+getUrl;

			location.href=url;
		});
	});
}
// 边框高亮提醒
function shake(ele,cls,times){
	var i = 0,t= false ,o =ele.attr("class")+" ",c ="",times=times||2;
	if(t) return;
	t= setInterval(function(){
		i++;
		c = i%2 ? o+cls : o;
		ele.attr("class",c);
		if(i==2*times){
			clearInterval(t);
			ele.removeClass(cls);
		}
	},200);
};
function del_address(id){
		swal({
			title: "确定删除此项记录?",
			text: "如果删除数据将不会恢复!",
			type: "warning",
			showCancelButton: true,
			confirmButtonColor: '#DD6B55',
			confirmButtonText: '是',
			cancelButtonText: "否",
			//closeOnConfirm: false,
			//closeOnCancel: false
		},
		function(isConfirm){
	    if (isConfirm){
			// 主域名
			var domain = "http://192.168.1.250/";
			// 设为默认-接口
			var add_address_url = domain+"mb/toDeleteConsignee.htmls";
				$.ajax({
				type: "GET",
				url: add_address_url,
					data: {
						user_id:$.cookie("user_id"),
						id:id,
						//user_id:$.cookie("user_id"),
					},
					dataType: "jsonp",
					jsonp: "callbackparam", //服务端用于接收callback调用的function名的参数  
					jsonpCallback: "success_jsonpCallback", //callback的function名称,服务端会把名称和data一起传递回来  
					success: function(json){
					vid="#item_"+id;
					$(vid).remove();
					}
				});
	    }
		});
	}

